<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者: 奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\admin\controller;
class Upload extends Base
{
public function initialize()
    {
        parent::initialize();
        
    }
 public function images(){
 //判断请求是否正确
    if(!$this->request->file('images')){ return error_json('图片上传接口：请求参数错误');}
    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('images');
    // 移动到框架应用根目录/uploads/ 目录下


    $images_path = !empty($settings['attachments']['images_path'])?$settings['attachments']['images_path']:config('app.upload_images.path');
    $images_ext  = !empty($settings['attachments']['images_ext'])?$settings['attachments']['images_ext']:config('app.upload_images.ext');
    $images_size = !empty($settings['attachments']['images_size'])?$settings['attachments']['images_size']:config('app.upload_images.size');

    foreach($files as $file){
          
          $info = $file->validate(['size'=> $images_size,'ext'=>$images_ext])->move(QL_ROOT.$images_path);
        if($info){
           $images =  $file->getInfo('images');


               return success_json('上传成功','/'.trim($images_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
               return error_json($file->getError());
        }

    }


 }


 public function videos(){
   if(!$this->request->file('videos')){ return error_json('视频上传接口：请求参数错误');}
    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('videos');
    // 移动到框架应用根目录/uploads/ 目录下
    // print_r($files);
    $videos_path = !empty($settings['attachments']['videos_path'])?$settings['attachments']['videos_path']:config('app.upload_videos.path');
    $videos_ext = !empty($settings['attachments']['videos_ext'])?$settings['attachments']['videos_ext']:config('app.upload_videos.ext');
    $videos_size = !empty($settings['attachments']['videos_size'])?$settings['attachments']['videos_size']:config('app.upload_videos.size');

    foreach($files as $file){
          $info = $file->validate(['size'=> $videos_size,'ext'=>$videos_ext])->move(QL_ROOT.$videos_path);
        if($info){
           $videos =  $file->getInfo('videos');

             return success_json('上传成功','/'.trim($videos_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
             return error_json($file->getError());
        }

    }


 }

//上传文件
 public function files(){
    if(!$this->request->file('files')){ return error_json('文件上传接口：请求参数错误');}
    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('files');
    // 移动到框架应用根目录/uploads/ 目录下
  
    $videos_path = !empty($settings['attachments']['files_path'])?$settings['attachments']['files_path']:config('app.upload_files.path');
    $files_ext = !empty($settings['attachments']['files_ext'])?$settings['attachments']['files_ext']:config('app.upload_files.ext');
    $files_size = !empty($settings['attachments']['files_size'])?$settings['attachments']['files_size']:config('app.upload_videos.size');

    foreach($files as $file){
          $info = $file->validate(['size'=> $files_size,'ext'=>$files_ext])->move(QL_ROOT.$files_path);
        if($info){
           $files =  $file->getInfo('files');
             return success_json('上传成功','/'.trim($files_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
             return error_json($file->getError());
        }

    }
  }
}